package cn.itcast.service.impl;

import cn.itcast.dao.CheckGroupDao;
import cn.itcast.dao.OrderDao;
import cn.itcast.entity.PageResult;
import cn.itcast.pojo.CheckGroup;
import cn.itcast.pojo.Setmeal;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import cn.itcast.dao.SetmealDao;
import cn.itcast.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealDao setmealDao;
    @Autowired
   private CheckGroupDao checkGroupDao;

    @Autowired
    private OrderDao orderDao;

    @Transactional
    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        //保存体检套餐基础数据
        setmealDao.add(setmeal);
        //保存体检套餐关联体检组数据
        if (checkgroupIds != null) {
            setCheckGroupAssociation(setmeal.getId(), checkgroupIds);
        }
    }

    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage, pageSize);
        Page<Setmeal> page = setmealDao.findByCondition(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public Integer[] selectedCheckGroupIds(Integer setMealId) {
        return setmealDao.findCheckGroupAssociationIds(setMealId);
    }

    @Override
    public Setmeal findById(Integer id) {
        Setmeal setmeal = setmealDao.findById(id);
        List<Integer> checkGroupId = setmealDao.findCheckGroupId(id);
        List<CheckGroup> List  = new ArrayList<>();
        for (Integer integer : checkGroupId) {

            CheckGroup checkGroup = checkGroupDao.findById(integer);
            List.add(checkGroup);
        }
        setmeal.setCheckGroups(List);
        return setmeal ;
    }



    @Transactional
    @Override
    public void update(Setmeal setmeal, Integer[] checkgroupIds) {
        //更新体检套餐基础信息
        setmealDao.update(setmeal);
        //删除之前体检套餐与体检组的关联信息
        setmealDao.deleteCheckGroupAssociation(setmeal.getId());
        //保存新的体检套餐与体检组的关联信息
        if (checkgroupIds != null) {
            setCheckGroupAssociation(setmeal.getId(), checkgroupIds);
        }
    }

    @Transactional
    @Override
    public void deleteById(Integer id) {
        //删除体检套餐与体检组的关联数据
        setmealDao.deleteCheckGroupAssociation(id);
        //删除体检套餐数据
        setmealDao.deleteById(id);
    }

    @Override
    public List<Setmeal> findAll() {
        return setmealDao.findAll();
    }

    @Override
    public Setmeal findSetmealDetail(Integer id) {
        return setmealDao.findDetailsById(id);
    }

    @Override
    public List<Map<String, Object>> findSetmealOrderedCount() {
        return setmealDao.findSetmealOrderedCount();
    }

    @Override
    public List<Setmeal> findAllBySex(Integer sex) {
        return setmealDao.findAllBySex(sex);
    }

    @Override
    public List<Map<String, Object>> findSetmealCount() {
        return null;
    }

    /**
     * 保存检查组和检查项关联信息
     *
     * @param setmealId
     * @param checkGroupIds
     */
    private void setCheckGroupAssociation(Integer setmealId, Integer[] checkGroupIds) {
        Stream.of(checkGroupIds).forEach(checkGroupId -> setmealDao.setCheckGroupAssociation(setmealId, checkGroupId));
    }



    @Override
    public Map<String, Object> reportSetMealCount() {
        //查询得到所有套餐对象
        List<Setmeal> setmealList = this.setmealDao.findAll();
        //套餐名字
        List<String> setmealNames = new ArrayList<>();
        //套餐名字+套餐预约数量
        List<Map<String,Object>> setmealCount=new ArrayList<>();
        //遍历集合 根据套餐id查询预约表预约人数
        for (Setmeal setmeal : setmealList) {
            Integer count = this.orderDao.selectMemberCount(setmeal.getId());
            //如果预约人数大于0 说明该套餐有预约就在图上进行展示
            if(count>0){
                setmealNames.add(setmeal.getName());
                Map<String,Object> map=new HashMap<>();
                //设置套餐名+套餐预约人数
                map.put("name",setmeal.getName());
                map.put("value",count);
                setmealCount.add(map);
            }
        }

        Map<String,Object> map1=new HashMap<>();
        map1.put("setmealCount",setmealCount);
        map1.put("setmealNames",setmealNames);
        return map1;

    }


}
